package pri.lt.parser;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class WebDriverManager {


    private static List<WebDriverWrapper> mWebDriverWrappers = new ArrayList<>();

    public static synchronized WebDriverWrapper getWebDriverWrapper() {
//        for (WebDriverWrapper wrapper : mWebDriverWrappers) {
//            if (!wrapper.isInUse()) {
//                //return wrapper;
//            }
//        }
        WebDriverWrapper webDriverWrapper = new WebDriverWrapper();
        webDriverWrapper.setId(mWebDriverWrappers.size() + 1);
        webDriverWrapper.setInUse(true);


//        ChromeOptions chromeOptions = new ChromeOptions();
//        chromeOptions.addArguments("--headless");
//        chromeOptions.addArguments("--load-images=no");
//        chromeOptions.addArguments("--ignore-ssl-errors=true");
//
//        WebDriver webDriver = new ChromeDriver(chromeOptions);
//        webDriver.manage().window().maximize();
//        webDriver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
//        webDriver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
//        webDriverWrapper.setWebDriver(webDriver);
//        //mWebDriverWrappers.add(webDriverWrapper);

        //设置必要参数
        DesiredCapabilities dcaps = DesiredCapabilities.chrome();
        //ssl证书支持
        dcaps.setCapability("acceptSslCerts", true);
        //截屏支持
        dcaps.setCapability("takesScreenshot", false);
        //css搜索支持
        dcaps.setCapability("cssSelectorsEnabled", true);
        //js支持
        dcaps.setJavascriptEnabled(true);

        //ua参数
        dcaps.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36");
        dcaps.setCapability("phantomjs.page.customHeaders.User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36");


        //图片支持
        dcaps.setCapability("phantomjs.page.settings.loadImages", false);


//        dcaps.setCapability("phantomjs.page.customHeaders.Connection", "keep-alive");
//        dcaps.setCapability("phantomjs.page.customHeaders.Cache-Control", "max-age=0");
//        dcaps.setCapability("phantomjs.page.customHeaders.Upgrade-Insecure-Requests", "1");
//        dcaps.setCapability("phantomjs.page.customHeaders.Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
//        dcaps.setCapability("phantomjs.page.customHeaders.Accept-Encoding", "gzip, deflate, br");
//        dcaps.setCapability("phantomjs.page.customHeaders.Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");



        //dcaps.setCapability("phantomjs.page.customHeaders.referer", "https://www.sina.com.cn/");

        //驱动支持
        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"/Users/litao/tools/phantomjs-2.1.1-macosx/bin/phantomjs");

        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--web-security=no", "--ignore-ssl-errors=yes", "--ssl-protocol=any", "--output-encoding=gbk", "--script-encoding=gbk"});


        PhantomJSDriver driver = new PhantomJSDriver(dcaps);
        driver.manage().window().maximize();
        driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

        webDriverWrapper.setWebDriver(driver);

        return webDriverWrapper;

    }

    public static synchronized void release(WebDriverWrapper webDriverWrapper) {
        try {

            webDriverWrapper.getWebDriver().switchTo().defaultContent();
            webDriverWrapper.getWebDriver().close();
            webDriverWrapper.getWebDriver().quit();
        } catch (Exception e) {
            try {

                webDriverWrapper.getWebDriver().quit();
            } catch (Exception e1) {

            }
        }
        webDriverWrapper.setInUse(false);
    }
}
